package com.shsmi.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.shsmi.util.ClassPathResourceReader;

@Service
public class NoodlesList {

    public static List<GeneralPath> noodlesList = new ArrayList<>();

    public static List<JSONObject> drawRegionCityList = new ArrayList<>();

    public static List<JSONObject> committeeList = new ArrayList<>();
    public static List<JSONObject> streetList = new ArrayList<>();
    public static List<JSONObject> gridList = new ArrayList<>();
    public static List<JSONObject> polygonList = new ArrayList<>();

    public static void communityNoodles() {
        addData("./static/xq_polygon.json", "P");
        addData("./static/居委会.json", "C");
        addData("./static/街道乡镇.json", "S");
        addData("./static/责任网格.json", "G");
    }

    public static void addData(String url, String type) {
//        String jsonStr=JsonFileUtil.readJsonFile(rul);
//        JSONObject jsonObject=JSONObject.parseObject(jsonStr);

        String jsonStr = new ClassPathResourceReader(url).getContent();

        JSONObject jsonObject = JSONObject.parseObject(jsonStr);

        JSONArray jsonArrayList = jsonObject.getJSONArray("features");
        for (Object obj : jsonArrayList) {
            JSONObject retJson = new JSONObject();
            JSONObject jsonObject1 = JSONObject.parseObject(obj.toString());
            //平面集合
            if (!jsonObject1.containsKey("geometry")) {
                continue;
            }
            JSONObject jsonObject2 = JSONObject.parseObject(jsonObject1.get("geometry").toString());
            //需返回的信息
            JSONObject jsonObject3 = JSONObject.parseObject(jsonObject1.get("properties").toString());
            JSONArray jsonArray = JSONArray.parseArray(jsonObject2.get("coordinates").toString());
            List<GeneralPath> noodles = new ArrayList<>();
            jsonArray.stream().parallel().forEach(jsonstrr -> {
                JSONArray jsonArray1 = JSONArray.parseArray(jsonstrr.toString());
                ArrayList<Point2D.Double> list = new ArrayList<>();
                for (Object jsstr : jsonArray1) {
                    JSONArray array = JSONArray.parseArray(jsstr.toString());
                    if (array.size() == 2) {
                        Point2D.Double point2D = new Point2D.Double(Double.parseDouble(array.get(0).toString()), Double.parseDouble(array.get(1).toString()));
                        list.add(point2D);
                    } else {
                        for (Object objj : array) {
                            JSONArray arrayy = JSONArray.parseArray(objj.toString());
                            Point2D.Double point2D = new Point2D.Double(Double.parseDouble(arrayy.get(0).toString()), Double.parseDouble(arrayy.get(1).toString()));
                            list.add(point2D);
                        }
                    }
                }
                noodles.add(genGeneralPath(list));
            });
            retJson.put("attributes", jsonObject3);
            retJson.put("noodlesList", noodles);
            if ("C".equals(type)) {
                committeeList.add(retJson);
            } else if ("S".equals(type)) {
                streetList.add(retJson);
            } else if ("G".equals(type)) {
                gridList.add(retJson);
            } else if ("P".equals(type)) {
                polygonList.add(retJson);
            }
        }
    }

    public static void drawRegionCity() {
//        String jsonStr=JsonFileUtil.readJsonFile("static/draw_region_city.json");

        String jsonStr = new ClassPathResourceReader("static/draw_region_city.json").getContent();

        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        JSONArray jsonArrayList = jsonObject.getJSONArray("features");
        jsonArrayList.stream().parallel().forEach(json -> {
            JSONObject retJson = new JSONObject();
            JSONObject jsonObject1 = JSONObject.parseObject(json.toString());
            //平面集合
            JSONObject jsonObject2 = JSONObject.parseObject(jsonObject1.get("geometry").toString());
            //需返回的信息
            JSONObject jsonObject3 = JSONObject.parseObject(jsonObject1.get("attributes").toString());
            JSONArray jsonArray = JSONArray.parseArray(jsonObject2.get("rings").toString());
            List<GeneralPath> noodles = new ArrayList<>();
            jsonArray.stream().parallel().forEach(jsonstrr -> {
                JSONArray jsonArray1 = JSONArray.parseArray(jsonstrr.toString());
                ArrayList<Point2D.Double> list = new ArrayList<>();
                for (Object jsstr : jsonArray1) {
                    JSONArray array = JSONArray.parseArray(jsstr.toString());
                    Point2D.Double point2D = new Point2D.Double(Double.parseDouble(array.get(0).toString()), Double.parseDouble(array.get(1).toString()));
                    list.add(point2D);
                }
                noodles.add(genGeneralPath(list));
            });
            retJson.put("attributes", jsonObject3);
            retJson.put("noodlesList", noodles);
            drawRegionCityList.add(retJson);
        });
    }

    /**
     * 将经纬度点集合转换为GeneralPath对象
     *
     * @param points 点集合(有序)
     * @return GeneralPath对象
     */
    public static GeneralPath genGeneralPath(List<Point2D.Double> points) {
        GeneralPath path = new GeneralPath();

        if (points.size() < 3) {
            return null;
        }

        path.moveTo((float) points.get(0).getX(), (float) points.get(0).getY());

        for (Iterator<Point2D.Double> it = points.iterator(); it.hasNext(); ) {
            Point2D.Double point = (Point2D.Double) it.next();

            path.lineTo((float) point.getX(), (float) point.getY());
        }

        path.closePath();

        return path;
    }

    public static void main(String[] args) {
        communityNoodles();
    }

}
